import $API from '@/api/services'
import { isWxBrowser } from '@/utils'
const shareurlSend = location.origin + location.pathname
const { API } = $globalconfig.URLS

const overView = {
  state: {
    ovvData: null,
    loadInfo: null
  },
  actions: {
    // 获取用户数据
    sendCustomInfoOvv({ commit }, userData) {
      if (!$globalconfig.isHaiMeng) {
        $API('getCustomInfoOvv', [userData])
      }
    },
    fetchOverview({ commit }, _id) {
      $API('getOverview', [_id]).then(res => {
        if (res.code == 200 || res.code == 100) {
          document.title = res.data.name
          if ($globalconfig.isHaiMeng) {
            if (res.data.bgmusicurl) {
              res.data.full_bgmusic_url = API + res.data.bgmusicurl.split('|')[1]
            }
            res.data.full_image_url = API + res.data.imageurl
            res.data.point_list = res.data.pointlist || []
            res.data.load_type = 0
            if (res.data.loadinfo) {
              res.data.loadinfo.full_pc_load_img = res.data.loadinfo.pc_load_img
              res.data.loadinfo.full_load_img = res.data.loadinfo.load_img
              res.data.load = res.data.loadinfo
            }
            if (res.data.startpicurl) {
              res.data.full_start_image_url = API + res.data.startpicurl
            }
          }
          // 动画数据
          let infoLoad = null
          if (res.data.load_type == 1 && res.data.fk_userpano_load_id) {
            infoLoad = {
              fk_userpano_load_id: res.data.fk_userpano_load_id,
              load_type: res.data.load_type,
            }
          } else {
            if (res.data.load) {
              res.data.load.pc_load_img = res.data.load.full_pc_load_img
              res.data.load.load_img = res.data.load.full_load_img
              res.data.loadinfo = res.data.load
              infoLoad = res.data.loadinfo
              infoLoad.load_type = res.data.load_type
              infoLoad.fk_userpano_load_id = res.data.fk_userpano_load_id
            }
          }
          this.dispatch('fetchOverviewLoadInfo', infoLoad)
          commit('SAVE_OVERVIEW_DATA', res.data)
        } else {
          window.$VM.$vgo.tip(res.msg, 'warning')
        }
      })
    },
    getOverviewWXShareData({ commit }) {
      if (!isWxBrowser()) return
      let ovvData = this.getters.ovvData
      let shareurl = window.location.href
      $API('getOverviewWXShare', [shareurlSend]).then(res => {
        let data = res.data
        if(data) {
          wx.config({
            debug: false,
            appId: data.appId,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: [
              'onMenuShareTimeline',
              'openLocation',
              'getLocation',
              'onMenuShareAppMessage',
              'onMenuShareQQ',
              'onMenuShareQZone',
              'onMenuShareWeibo',
              'showOptionMenu'
            ]
          })

          let shareContent = {
            title: ovvData.name,
            desc: ovvData.contents || '',
            link: shareurl,
            imgUrl: API + ovvData.smallimageurl,
            success: function() {},
            cancel: function() {}
          }

          window.wx.ready(function() {
            wx.showOptionMenu()

            wx.onMenuShareTimeline(shareContent)

            //分享给朋友
            wx.onMenuShareAppMessage(shareContent)

            //QQ
            wx.onMenuShareQQ(shareContent)

            //QQ微博
            wx.onMenuShareWeibo(shareContent)

            //QQ空间
            wx.onMenuShareQZone(shareContent)

            window.$VM.$vgo.emit('listenWXLoad')
          })

          window.wx.error(function(res) {
            console.log(res)
          })
        }
      })
    }
  },
  mutations: {
    SAVE_OVERVIEW_DATA: (state, data) => {
      state.ovvData = data
    }
  }
}

export default overView
